/**
  ******************************************************************************
* @file				bsp_uart.c
* @Software			MDK V5
* @Target 			CW32F0300
* @author			ou_teki
* @encoding			UTF-8
* @date				2024-0715
  ******************************************************************************
  */


#include	"uart_com.h"

//-TKW24-0715-Uart Init
//-UART1_TX_PA9  UART1_RX_PA10 
uint8_t    UartComInit(void)
{
	__RCC_GPIOA_CLK_ENABLE();
	__RCC_UART1_CLK_ENABLE();

	PA09_AFx_UART1RXD();
	PA08_AFx_UART1TXD();
	
	GPIO_InitTypeDef	gpio_init;
	gpio_init.IT = GPIO_IT_NONE;
	gpio_init.Speed = GPIO_SPEED_HIGH;
	gpio_init.Mode = GPIO_MODE_INPUT_PULLUP;  //RXD
	gpio_init.Pins = GPIO_PIN_9;
	GPIO_Init(CW_GPIOA, &gpio_init);
	gpio_init.Mode = GPIO_MODE_OUTPUT_PP;  //TXD
	gpio_init.Pins = GPIO_PIN_8;
	GPIO_Init(CW_GPIOA, &gpio_init);

	USART_InitTypeDef	usart_init;
	usart_init.USART_BaudRate = 9600;
	usart_init.USART_StartBit = USART_StartBit_FE;
	usart_init.USART_StopBits = USART_StopBits_1_5;
	usart_init.USART_Parity = USART_Parity_No;
	usart_init.USART_Over = USART_Over_16;
	usart_init.USART_Source = USART_Source_PCLK;
	usart_init.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
    usart_init.USART_UclkFreq = 24000000;////����Ҫ��
	usart_init.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_Init(CW_UART1, &usart_init);
//		USART_DirectionModeCmd(CW_UART3, USART_Mode_Tx, ENABLE);
//		USART_DirectionModeCmd(CW_UART3, USART_Mode_Rx, ENABLE);
	
	USART_ITConfig(CW_UART1, USART_IT_RC, ENABLE);
	NVIC_EnableIRQ(UART1_IRQn);

    return 0;
}


uint8_t    UartComPrintf(char *fmt,...)
{
    va_list arg_ptr;
    uint8_t len = 0;
	uint8_t i = 0;
    char str[0XFF];

    va_start(arg_ptr, fmt);
    len = vsprintf(str, fmt, arg_ptr);
    va_end(arg_ptr);
	
    for (i=0; i<len; i++)
    {
		USART_SendData_8bit(CW_UART1, str[i]);
		while(USART_GetFlagStatus(CW_UART1, USART_FLAG_TXE) == RESET);
    }
    
    return 0;
}

uint8_t    uart_com_data[0XFF];
uint8_t    uart_com_data_len = 0;
uint8_t    uart_com_flag = 0;
void	UART1_IRQHandler(void)
{
	if( USART_GetITStatus(CW_UART1, USART_IT_RC) )
	{
		USART_ClearITPendingBit(CW_UART1, USART_IT_RC);	
		////
//		uart_byte = USART_ReceiveData_8bit(CW_UART1);
//		UartSend(uart_byte);
		////
		// if( 0 == uart_star_flag )
		// {
		// 	uart_star_flag = 1;
		// 	uart_len = 0;
		// 	time_num = 0;
		// }
		// if( 1 == uart_star_flag )
		// {
		// 	uart_byte	= USART_ReceiveData_8bit(CW_UART1);
		// 	uart_date[uart_len] = uart_byte;
		// 	uart_len++;
		// }
	}
}




/********************************End of File************************************/

